﻿using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SZF.WebParts.LrgBanner
{
    [ToolboxItemAttribute(false)]
    public partial class LrgBanner : WebPart
    {
        public LrgBanner()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            //Apply width and height to the banner
            String CssLiteral = String.Format("<style> #{0} .BnrContent .slider li {{width: {1}px; height: {2}px;}}</style>",
                                                this.ClientID,
                                                BannerWidth,
                                                BannerHeight);

            this.Controls.Add(new Literal { Text = CssLiteral });

            this.Page.ClientScript.RegisterClientScriptInclude("easySlider1.7.js", "/_Layouts/SZF.WebParts/Scripts/easySlider1.7.js");
            this.Page.ClientScript.RegisterClientScriptInclude("SliderHelper.js", "/_Layouts/SZF.WebParts/Scripts/SliderHelper.js");
            //TODO: 2) Создать ImageList Definition c URL, Description, Show
            //TODO: 5) Сделать возможность выбора SPWeb
            //TODO: 6) Перенести стили с  высотой и шириной баннеров в общий файл стилей.
            //getBannerImages(null, 'Images2', 'Folder','Show', 'URL', 'Comments',  '#slider', { auto: true, speed: 400, pause: 5000, controlsShow: false, continuous: true, vertical: true });
            string SliderScript = string.Empty;
            string SliderSettings = 
                String.Format("{{ auto: true, speed: {0}, pause: {1}, controlsShow: false, continuous: true, vertical: {2} }}",
            Speed, 
            Pause, 
            Vertical.ToString().ToLower());

            SliderScript = String.Format(@" 
                         $(document).ready(function () {{
                            getBannerImages(null, '{0}', '{1}', '{2}', '{3}', '{4}', '#{5} #slider', {6} );
                         }});
                       ", SPLibDisplayName, SPFolderName, ShowColumnName, UrlColumnName, CommentsColumnName, this.ClientID, SliderSettings);

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID, SliderScript, true);
        }

        protected override void CreateChildControls()
        {
                base.CreateChildControls();
        }
        #region Properties
        
       [WebBrowsable(true),  
       Category("Banner Settings"),  
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Transition time in milliseconds"),  
       WebDescription("Number in milliseconds")]
        public int Speed { get { return _Speed; } set { _Speed = value; } }
       private int _Speed = 200;

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Pause in milliseconds"),
       WebDescription("How many milliseconds it will be shows between transitions")]
       public int Pause { get { return _Pause; } set { _Pause = value; } }
       private int _Pause = 2000;

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Vertical slider"),
       WebDescription("Yes - images slide vertically. No - horizontally")]
       public bool Vertical { get { return _Vertical; } set { _Vertical = value; } }
       private bool _Vertical = true;

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Banner height"),
       WebDescription("Height in pixels")]
       public int BannerHeight { get { return _BannerHeight; } set { _BannerHeight = value; } }
       private int _BannerHeight = 100;

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Banner width"),
       WebDescription("Width in pixels")]
       public int BannerWidth { get { return _BannerWidth; } set { _BannerWidth = value; } }
       private int _BannerWidth = 100;

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Relative Web URL"),
       WebDescription("Relative Web URL, that contains a library with images")]
       public string SpWebDisplayName { get { return _SpWebDisplayName; } set { _SpWebDisplayName = value; } }
       private string _SpWebDisplayName = "/";

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Library Display Name (without spaces!)"),
       WebDescription("")]
       public string SPLibDisplayName { get { return _SPLibDisplayName; } set { _SPLibDisplayName = value; } }
       private string _SPLibDisplayName = "ImageBanners";

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
       public string SPFolderName { get { return _SPFolderName; } set { _SPFolderName = value; } }
       private string _SPFolderName = "BigBanner";

       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Name of the URL Column"),
       WebDescription("Column that stores the target url that will be open when clicking the picture")]
       public string UrlColumnName { get { return _UrlColumnName; } set { _UrlColumnName = value; } }
       private string _UrlColumnName = "URL";
         
       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Column name dedicated for Comments"),
       WebDescription("Column name dedicated for Comments (does not work yet)")]
       public string CommentsColumnName { get { return _CommentsColumnName; } set { _CommentsColumnName = value; } }
       private string _CommentsColumnName = "Comments";


       [WebBrowsable(true),
       Category("Banner Settings"),
       Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Coulm name used as a flag"),
       WebDescription("Boolean field. True = show in slider web part. No - do not show")]
       public string ShowColumnName { get { return _ShowColumnName; } set { _ShowColumnName = value; } }
       private string _ShowColumnName = "Show";
      #endregion

    }
}
